// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   TestGSClient.java

package com.succez.custom.gssjxt.client;

import com.gs.szhy.api.DataProvide;
import com.gs.szhy.api.Package;
import java.io.IOException;
import java.io.PrintStream;
import java.util.*;
import java.util.concurrent.*;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.dom4j.Document;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// Referenced classes of package com.succez.custom.gssjxt.client:
//            ConfigUtil, ServiceClientFactory, WebServiceUtil

public class TestGSClient
    implements Runnable
{

    public static void main(String args[])
        throws IOException
    {
        start();
    }

    public static void start()
    {
        ConfigUtil.initConfig();
        pool = Executors.newScheduledThreadPool(1);
        TestGSClient client = new TestGSClient();
        pool.scheduleWithFixedDelay(client, 0L, 5L, TimeUnit.SECONDS);
    }

    public static void stop()
    {
        pool.shutdown();
    }

    public TestGSClient()
    {
        user = "DS420000";
        password = "123";
        version = "1.10";
        reqcount = 0;
        objMapper = new ObjectMapper();
        try
        {
            instance = ServiceClientFactory.createInstance(ConfigUtil.url);
            version = WebServiceUtil.getVersion(version, ConfigUtil.backup);
            user = ConfigUtil.username;
            password = ConfigUtil.password;
        }
        catch(Exception ex)
        {
            throw new RuntimeException(ex);
        }
    }

    public void run()
    {
        try
        {
            getPackage();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void getPackage(String val)
    {
        try
        {
            if(val == null || "".equals(val))
                val = "1.10";
            String strs[] = val.split("\\|");
            String ver = strs[0];
            String tuser = user;
            String tpassword = password;
            if(strs.length > 1)
                tuser = strs[1];
            if(strs.length > 2)
                tpassword = strs[2];
            System.out.println((new StringBuilder("\u4E09\u8BC1\u5408\u4E00\u8BF7\u6C42\uFF1A{")).append(reqcount++).append("}").toString());
            Package pkg = instance.getPackage(tuser, tpassword, ver);
            if(pkg != null)
            {
                Document doc = WebServiceUtil.unzip2doc(pkg.getContent());
                System.out.println(doc.asXML());
            }
        }
        catch(Throwable ex)
        {
            ex.printStackTrace();
            log.error("\u8BF7\u6C42\u6570\u636E\u5305", ex);
        }
    }

    public void getPackage()
    {
        try
        {
            System.out.println((new StringBuilder("\u4E09\u8BC1\u5408\u4E00\u8BF7\u6C42\uFF1A{")).append(reqcount++).append("}").toString());
            Package pkg = instance.getPackage(user, password, version);
            if(pkg != null)
            {
                Document doc = WebServiceUtil.unzip2doc(pkg.getContent());
                if(feedBack(doc.asXML(), version))
                {
                    List nodes = doc.selectNodes("//Record/INFOACTIONTYPE");
                    int result[] = new int[4];
                    for(Iterator iterator = nodes.iterator(); iterator.hasNext();)
                    {
                        Element elem = (Element)iterator.next();
                        result[NumberUtils.toInt(elem.getText()) - 1]++;
                    }

                    Map feedbk = new HashMap();
                    feedbk.put("reg", Integer.valueOf(result[0]));
                    feedbk.put("alt", Integer.valueOf(result[1]));
                    feedbk.put("can", Integer.valueOf(result[2]));
                    feedbk.put("oth", Integer.valueOf(result[3]));
                    feedbk.put("name", pkg.getName());
                    feedbk.put("version", pkg.getVersion());
                    String feedbkJson = objMapper.writeValueAsString(feedbk);
                    System.out.println((new StringBuilder("feedbkJson\uFF1A")).append(feedbkJson).toString());
                    instance.feedback(user, password, feedbkJson);
                    version = pkg.getVersion();
                    WebServiceUtil.saveVersion(version, ConfigUtil.backup);
                    System.out.println((new StringBuilder("version\uFF1A{")).append(version).append("}").toString());
                }
            }
        }
        catch(Throwable ex)
        {
            ex.printStackTrace();
            log.error("\u8BF7\u6C42\u6570\u636E\u5305", ex);
        }
    }

    public void sendCleanTax()
        throws Exception
    {
        List taxInfos = new ArrayList();
        Random rand = new Random(100L);
        for(int i = 0; i < 10; i++)
        {
            Map taxInfo = new HashMap();
            taxInfo.put("uniscid", UUID.randomUUID().toString().replace("-", ""));
            taxInfo.put("orgno", UUID.randomUUID().toString().replace("-", ""));
            taxInfo.put("entname", (new StringBuilder("\u4F01\u4E1A")).append(rand.nextInt()).toString());
            taxInfo.put("cleantax", Math.random() <= 0.5D ? "2" : "1");
            taxInfo.put("customsettlement", Math.random() <= 0.5D ? "2" : "1");
            taxInfo.put("cleanbondunit", (new StringBuilder("\u5355\u4F4D")).append(rand.nextInt()).toString());
            taxInfo.put("cancellationcertificate", Math.random() <= 0.5D ? "1" : "0");
            taxInfos.add(taxInfo);
        }

        String resultInfo = instance.sendCleanTaxInfo(user, password, objMapper.writeValueAsString(taxInfos));
        Map result = (Map)objMapper.readValue(resultInfo, Map.class);
        System.out.println(resultInfo);
    }

    public boolean feedBack(String xml, String version)
    {
        Map param = new LinkedHashMap();
        param.put("bwlx", "GSXX");
        param.put("xml", StringEscapeUtils.escapeXml(xml));
        param.put("fileName", version);
        String wsdl = ConfigUtil.feedBack;
        String ns = "http://ws.dc.com";
        String method = ConfigUtil.feedBackMethod;
        try
        {
            String result = WebServiceUtil.accessService(wsdl, ns, method, param, "ns:return");
            System.out.println(result);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private static final Logger log = LoggerFactory.getLogger(TestGSClient.class);
    private DataProvide instance;
    private static final String FB_REG = "reg";
    private static final String FB_ALT = "alt";
    private static final String FB_CAN = "can";
    private static final String FB_OTH = "oth";
    private static final String TAX_UNISCID = "uniscid";
    private static final String TAX_ORGNO = "orgno";
    private static final String TAX_ENTNAME = "entname";
    private static final String TAX_CLEANTAX = "cleantax";
    private static final String TAX_CLEANBONDUNIT = "cleanbondunit";
    private static final String TAX_CUSTOMSETTLEMENT = "customsettlement";
    private static final String TAX_CANCELLATIONCERTIFICATE = "cancellationcertificate";
    private String user;
    private String password;
    private String version;
    private int reqcount;
    private ObjectMapper objMapper;
    private static ScheduledExecutorService pool;

}
